package com.contro.ht.controller;

import com.contro.ht.common.config.minio.MinioTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("minio")
public class MinioController {

    @Autowired
    private MinioTemplate minioTemplate;

    @Value("${minio.server}")
    private String server;

    @Value("${minio.port}")
    private Integer port;

    @Value("${minio.bucket}")
    private String bucket;


    /**
     * 单文件上传
     * @param file 文件
     * @return
     */
    @PostMapping("/uploadone")
    public Object uploadOne(MultipartFile file) {
        return minioTemplate.upload(file);
    }

    /**
     *  单文件下载
     * @param fileName 文件
     * @param delete 下载完后是否删除, 请谨慎传参
     */
    @GetMapping("/download")
    public void download(@RequestParam(value = "fileName") String fileName,
                         @RequestParam(defaultValue = "false")Boolean delete, HttpServletResponse response) {
        minioTemplate.fileDownload(fileName,delete,response);
    }

    /**
     * 查看存储的文件列表
     * @param bucket 桶
     * @return
     */
    @GetMapping("/list")
    public  Object fileList(@RequestParam(value = "bucket") String bucket) {
        return minioTemplate.listObjects(bucket);
    }

    /**
     * 删除文件
     * @param bucket 桶
     * @param list 文件名列表
     * @return
     */
    @DeleteMapping("/remove")
    public  Object fileremove(@RequestParam String bucket,@RequestParam List<String> list) {
        return minioTemplate.removeObjects(bucket,list);
    }

    /**
     * 搭配nginx测试服务测试集群下的效果
     * @return
     */
    @GetMapping ("/read")
    public  Object readPort() {
        return "欢迎访问服务8081端口项目!!";
    }

}
